<%@ include file="/include/taglib.jsp"%>
<c:set var="itemAction" value="Index" scope="request"/>
<html>
<head>
<title>${modelPluralName}</title>
</head>
<body>
	<script type="text/javascript">
	function pageinit() {
		$("#Add${modelName}Form").validate();
	}
	</script>
	
	<h1>${modelPluralName}</h1>
	<div class="form_add">
		<c:choose>
			<c:when test="${not empty user and empty addLink}">
				<form id="Add${modelName}Form" action="/page/item/${modelName}/${modelName}Add.jsp" data-transition="slidedown" method="post">
					<div class="ui-grid-a">
						<div class="ui-block-a">
							<div data-role="fieldcontain" class="ui-hide-label ItemAddNameBlock">
								<label for="${modelName}_name" data-inline="true">Name:</label>
								<input type="text" name="name" id="${modelName}_name" placeholder="${modelDisplayName} Name" class="required" />
							</div>
						</div>
						<div class="ui-block-b">
							<button type="submit" data-role="button" data-icon="plus">Add ${modelDisplayName}</button>
						</div>
					</div>
				</form>
			</c:when>
			<c:when test="${not empty addLink}">
				<a href="${fn:escapeXml(addLink)}" data-role="button" data-icon="plus">${fn:escapeXml(addLinkName)}</a>
			</c:when>
		</c:choose>
	</div>
	<c:choose>
		<c:when test="${not empty user}">
			<c:set var="deleteIcon" value="delete" scope="request"/>
		</c:when>
		<c:otherwise>
			<c:set var="deleteIcon" value="arrow-r" scope="request"/>
		</c:otherwise>
	</c:choose>
	<c:choose>
		<c:when test="${empty user && not empty items && not empty items[0].indexColumns}">
			<ul data-role="listview" data-split-icon="${deleteIcon}" data-split-theme="d">
				<c:set var="itemLevel" value="1" scope="request"/>
				<c:forEach var="item" items="${items}">
					<c:if test="${not empty user or item.approved}">
						<li>
							<a href="/page/item/${modelName}/${modelName}View.jsp?id=${item.id}"> 
								<c:if test="${not empty item.photo}">
									<img src="${item.photo.uriPrefix}80${item.photo.uriSuffix}" height="${tate:toInt(item.photo.heightRatio * (item.photo.maxSize gt 80 ? 80 : item.photo.maxSize))}" width="${tate:toInt(item.photo.widthRatio * (item.photo.maxSize gt 80 ? 80 : item.photo.maxSize))}" alt="${fn:escapeXml(item.photo.description)}" />
								</c:if>
								<c:forEach var="itemField" items="${item.fields}">
									<c:if test="${itemField.inIndex}">
										<c:set var="itemField" value="${itemField}" scope="request"/>
										<c:set var="item" value="${item}" scope="request"/>
										<% pageContext.setAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("itemField", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
										<% pageContext.setAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("item", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
										<c:set var="editableClass" value="" scope="request"/>
										<c:set var="itemLevel" value="${itemLevel + 1}" scope="request"/>
										<jsp:include page="/fragment/ItemField.jsp"/>
										<c:set var="itemLevel" value="${itemLevel - 1}" scope="request"/>
										<% pageContext.setAttribute("itemField", pageContext.getAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
										<% pageContext.setAttribute("item", pageContext.getAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
									</c:if>
								</c:forEach>
							</a>
							<c:if test="${not empty user and not item.approved}">
								<div>
									<a href="#" data-role="button" data-icon="check" data-inline="true">Approve</a>
								</div>
							</c:if>
							<c:choose>
								<c:when test="${not empty user}">
									<a href="/fragment/ItemDelete.jsp?modelName=${fn:escapeXml(modelName)}&amp;modelDisplayName=${fn:escapeXml(modelDisplayName)}&amp;name=${fn:escapeXml(item.name)}&amp;id=${item.id}&amp;uri=${tate:encodeUri(tate:getRequestUriAndBeyond(pageContext.request))}" data-rel="dialog" data-transition="slideup">Delete ${fn:escapeXml(modelDisplayName)}</a>
								</c:when>
								<c:otherwise>
									<a href="/page/item/${modelName}/${modelName}View.jsp?id=${item.id}">View ${fn:escapeXml(modelDisplayName)}</a>
								</c:otherwise>
							</c:choose>
						</li>
					</c:if>
				</c:forEach>
			</ul>
		</c:when>
		<c:otherwise>
			<ul data-role="listview" data-split-icon="${deleteIcon}" data-split-theme="d">
				<c:set var="itemLevel" value="1" scope="request"/>
				<c:forEach var="item" items="${items}">
					<c:if test="${not empty user or item.approved}">
						<li>
							<c:if test="${not item.approved}">
								<div class="ui-grid-a">
								<div class="ui-block-a">
								<c:if test="${not empty user}">
								<a href="/page/item/${modelName}/${modelName}Edit.jsp?id=${item.id}&amp;field=approved&amp;value=true&amp;uri=${tate:encodeUri(tate:getRequestUriAndBeyond(pageContext.request))}" data-role="button" data-icon="check" data-inline="true">Approve</a>
								</c:if>
								<a href="/fragment/ItemDelete.jsp?modelName=${fn:escapeXml(modelName)}&amp;modelDisplayName=${fn:escapeXml(modelDisplayName)}&amp;name=${tate:encodeUri(item.name)}&amp;id=${item.id}&amp;uri=${tate:encodeUri(tate:getRequestUriAndBeyond(pageContext.request))}" data-rel="dialog" data-transition="slideup" data-role="button" data-icon="delete" data-inline="true">Delete ${fn:escapeXml(modelDisplayName)}</a>
							</c:if>
							<c:if test="${not item.approved}">
								</div>
								<div class="ui-block-b">
							</c:if>
							<a href="/page/item/${modelName}/${modelName}View.jsp?id=${item.id}"> 
								<c:if test="${not empty item.photo}">
									<img src="${item.photo.uriPrefix}80${item.photo.uriSuffix}" height="${tate:toInt(item.photo.heightRatio * (item.photo.maxSize gt 80 ? 80 : item.photo.maxSize))}" width="${tate:toInt(item.photo.widthRatio * (item.photo.maxSize gt 80 ? 80 : item.photo.maxSize))}" alt="${fn:escapeXml(item.photo.description)}" />
								</c:if>
								<c:forEach var="itemField" items="${item.fields}">
									<c:if test="${itemField.inIndex}">
										<c:set var="itemField" value="${itemField}" scope="request"/>
										<c:set var="item" value="${item}" scope="request"/>
										<% pageContext.setAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("itemField", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
											<% pageContext.setAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), pageContext.getAttribute("item", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
										<c:set var="editableClass" value="" scope="request"/>
										<c:set var="itemLevel" value="${itemLevel + 1}" scope="request"/>
										<jsp:include page="/fragment/ItemField.jsp"/>
										<c:set var="itemLevel" value="${itemLevel - 1}" scope="request"/>
										<% pageContext.setAttribute("itemField", pageContext.getAttribute("itemField" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
										<% pageContext.setAttribute("item", pageContext.getAttribute("item" + pageContext.getAttribute("itemLevel", PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE), PageContext.REQUEST_SCOPE); %>
									</c:if>
								</c:forEach>
							</a>
							<c:if test="${not item.approved}">
								</div>
								</div>
							</c:if>
						</li>
					</c:if>
				</c:forEach>
			</ul>
		</c:otherwise>
	</c:choose>
</body>
</html>
